package com.android.backup;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.android.contacts.ContactsApiSdk5.ContactsBackCallback;
import com.android.contacts.model.ContactsConstants;
import com.android.contacts.model.JExcelController;
import com.android.contacts.model.vo.Contact;
import com.android.contacts.service.ServiceDelegate;
import com.android.utility.Utils;

public class AllBackupActivity extends Activity implements ContactsBackCallback {

	private final String TAG = AllBackupActivity.class.getSimpleName();
	private final int NO_SDCARD_ID = 901;
	private final int NO_CONTACT_ID = 902;
	private final int CONTACT_BACKUP = 101;
	private final int SMS_BACKUP = 102;
	private final int ALL_BACKUP = 103;
	private ContactsBackCallback mContactsBackCallback;
	private JExcelController mJExcelController;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContactsBackCallback = this;
		setContentView(R.layout.all_backup);
		mJExcelController = new JExcelController(
				ContactsConstants.WORK_BOOK_NAME);
	}

	public void onClick(View view) {
		if (view.getId() == R.id.contacts) {
			doBackup(CONTACT_BACKUP);
		} else if (view.getId() == R.id.sms_backup) {
			doBackup(SMS_BACKUP);
		} else if (view.getId() == R.id.all_backup) {
			doBackup(ALL_BACKUP);
		}
	}

	private void doBackup(int id) {
		if (id == CONTACT_BACKUP) {
			if (!Utils.checkExternalMedia()) {
				showDialog(NO_SDCARD_ID);
			} else {
				addData();
			}

		} else if (id == SMS_BACKUP) {
			if (!Utils.checkExternalMedia()) {
				showDialog(NO_SDCARD_ID);
			} else {
			}
		} else if (id == ALL_BACKUP) {
			if (!Utils.checkExternalMedia()) {
				showDialog(NO_SDCARD_ID);
			} else {
			}
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {

		if (id == CONTACT_BACKUP) {
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			dialog.setIcon(android.R.drawable.ic_dialog_alert);
			dialog.setTitle("No Network Connection.");
			dialog.setPositiveButton("Ok", mCancleListener);
			dialog.create();
			return dialog.show();
		} else if (id == NO_CONTACT_ID) {
			AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			dialog.setIcon(android.R.drawable.ic_dialog_alert);
			dialog.setTitle("Information");
			dialog.setNegativeButton("Cancle", mCancleListener);
			dialog.create();
			return dialog.show();
		} else {
			return super.onCreateDialog(id);
		}
	}

	/**
	 * 
	 * @param index
	 * @return
	 */
	private String getEmailType(String index) {
		if (index.trim().equals("1")) {
			return "home";
		} else if (index.trim().equals("2")) {
			return "mobile";
		}
		if (index.trim().equals("3")) {
			return "work";
		}
		if (index.trim().equals("7")) {
			return "other";
		} else {
			return "?";
		}
	}

	/**
	 * 
	 * @param index
	 * @return
	 */
	private String getPhoneType(String index) {
		if (index.trim().equals("1")) {
			return "home";
		} else if (index.trim().equals("2")) {
			return "mobile";
		}
		if (index.trim().equals("3")) {
			return "work";
		}
		if (index.trim().equals("4")) {
			return "other";
		} else {
			return "?";
		}
	}

	private void addData() {
		(new Thread() {
			public void run() {
				ArrayList<Contact> contactList = ServiceDelegate.getInstance()
						.getContactList(true, "", getContentResolver(),
								mContactsBackCallback);
				if (contactList.size() == 0) {
					showDialog(NO_CONTACT_ID);
				} else {
					mJExcelController
							.createWorkSheet(ContactsConstants.WORK_SHEET_CONTACT);
					mJExcelController.createCellNames();
				}
			}
		}).start();

	}

	private DialogInterface.OnClickListener mCancleListener = new DialogInterface.OnClickListener() {

		public void onClick(DialogInterface dialog, int which) {

		}
	};

	public void saveContact(final String msg) {
		runOnUiThread(new Runnable() {

			public void run() {
				Toast.makeText(AllBackupActivity.this, msg, Toast.LENGTH_SHORT);

			}
		});
	}
}